<template>
  <div class="outerBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>重置密码</span>
      </div>
      <div class="innerBox">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="原密码" >
            <el-input type="password" v-model="ruleForm.originPass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pass">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm">修改密码</el-button>
            <el-button @click="resetForm">直接点击重置密码</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { changeUserPass } from '@/api'

export default {
  name: 'change-user-password',
  data () {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        pass: '',
        checkPass: '',
        originPass: ''
      },
      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async submitForm () {
      const data = {
        old_pwd: this.ruleForm.originPass,
        new_pwd: this.ruleForm.pass,
        re_pwd: this.ruleForm.checkPass
      }
      console.log(data)
      const res = await changeUserPass(data)
      this.$store.commit('SAVEUSERPASS', this.ruleForm.pass)
      if (res.data.code === 0) {
        this.$message.error(res.data.message)
      } else if (res.data.code === 1) {
        this.$message.error(res.data.message)
      } else {
        this.$message.error('输入的不能为空！')
      }
    },
    async resetForm () {
      const data = {
        old_pwd: this.$store.state.userPss,
        new_pwd: '111111',
        re_pwd: '111111'
      }
      const res = await changeUserPass(data)
      this.$store.commit('SAVEUSERPASS', '111111')
      if (res.data.message === '更新密码成功！') {
        this.$message.success(`初始化密码成功！密码为${this.$store.state.userPss}`)
      } else {
        this.$message.error(`已是初始化密码，请勿重复操作！密码为${this.$store.state.userPss}`)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.outerBox{
  width: 100%;
  height: 500px;
}
.innerBox{
  width: 400px;
}
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 30px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both
}

.box-card {
  width: 98%;
  margin: 20px;
  height: 400px;
}
</style>
